## knowledge_base.py

from typing import Dict, Any, List
from elasticsearch_client import ElasticsearchClient
import logging
from config import ELASTICSEARCH_KNOWLEDGE_BASE_INDEX

class KnowledgeBase:
    def __init__(self, es_client: ElasticsearchClient):
        """Initialize the KnowledgeBase with an ElasticsearchClient instance.

        Args:
            es_client: The ElasticsearchClient instance to interact with Elasticsearch.
        """
        self.es_client = es_client

    def fetch_data(self, query: str) -> List[Dict[str, Any]]:
        """Fetch data from the knowledge base based on a query.

        Args:
            query: The query string to search for in the knowledge base.

        Returns:
            A list of dictionaries containing the fetched data. If no data is found, returns an empty list.
        """
        # Construct the Elasticsearch query
        es_query = {
            "query": {
                "match": {
                    "content": query
                }
            }
        }
        # Query the Elasticsearch index
        results = self.es_client.query_index(ELASTICSEARCH_KNOWLEDGE_BASE_INDEX, es_query)
        if not results:
            logging.info(f"No data found for query: {query}")
        return results

    def delete_entry(self, entry_id: str) -> bool:
        """Delete an entry from the knowledge base.

        Args:
            entry_id: The ID of the entry to delete.

        Returns:
            True if the entry was deleted successfully, False otherwise.
        """
        # Delete the entry from the Elasticsearch index
        return self.es_client.delete_index_entry(ELASTICSEARCH_KNOWLEDGE_BASE_INDEX, entry_id)
